package com.its.web;

import com.its.pojo.DeleteMessage;
import com.its.pojo.Student;
import com.its.service.StudentService;
import com.its.util.HttpStatusEnum;
import com.its.util.Result;
import io.swagger.annotations.Api;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 朱厚强
 * @since 2024年01月16日
 */
@RestController
@RequestMapping("/student")
@Api(tags = "学生 -api")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @PostMapping("add")
    public Result add(@RequestBody Student student){
        // 添加信息
        boolean save = studentService.save(student);
        if (save){
            rabbitTemplate.convertAndSend("zhuhq.TOPIC","apply.add",student);
            return Result.ok().code(HttpStatusEnum.SUCCESS.getCode()).message("添加成功");
        }else {
            return Result.error().code(HttpStatusEnum.ERROR.getCode()).message("添加失败");
        }
    }
    @GetMapping("delete")
    public Result delete(@RequestParam String id){
        // 删除信息
        boolean save = studentService.removeById(id);
        if (save){
            rabbitTemplate.convertAndSend("zhuhq.TOPIC","apply.del",
                    new DeleteMessage().setId(Integer.valueOf(id)).setMessage("正常删除"));
            return Result.ok().code(HttpStatusEnum.SUCCESS.getCode()).message("删除成功");
        }else {
            return Result.error().code(HttpStatusEnum.ERROR.getCode()).message("删除失败");
        }
    }
}
